package ar.org.sstuyv

import grails.converters.JSON

import org.springframework.dao.DataIntegrityViolationException


class ReferenteController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [referenteInstanceList: Referente.list(params), referenteInstanceTotal: Referente.count()]
    }

    def create() {
        [referenteInstance: new Referente(params)]
    }

    def save() {
        def referenteInstance = new Referente(params)
        if (!referenteInstance.save(flush: true)) {
            render(view: "create", model: [referenteInstance: referenteInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'referente.label', default: 'Referente'), referenteInstance.id])
        redirect(action: "show", id: referenteInstance.id)
    }

    def show(Long id) {
        def referenteInstance = Referente.get(id)
        if (!referenteInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'referente.label', default: 'Referente'), id])
            redirect(action: "list")
            return
        }

        [referenteInstance: referenteInstance]
    }

    def edit(Long id) {
        def referenteInstance = Referente.get(id)
        if (!referenteInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'referente.label', default: 'Referente'), id])
            redirect(action: "list")
            return
        }

        [referenteInstance: referenteInstance]
    }

    def update(Long id, Long version) {
        def referenteInstance = Referente.get(id)
        if (!referenteInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'referente.label', default: 'Referente'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (referenteInstance.version > version) {
                referenteInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'referente.label', default: 'Referente')] as Object[],
                          "Another user has updated this Referente while you were editing")
                render(view: "edit", model: [referenteInstance: referenteInstance])
                return
            }
        }

        referenteInstance.properties = params

        if (!referenteInstance.save(flush: true)) {
            render(view: "edit", model: [referenteInstance: referenteInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'referente.label', default: 'Referente'), referenteInstance.id])
        redirect(action: "show", id: referenteInstance.id)
    }

    def delete(Long id) {
        def referenteInstance = Referente.get(id)
        if (!referenteInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'referente.label', default: 'Referente'), id])
            redirect(action: "list")
            return
        }

        try {
            referenteInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'referente.label', default: 'Referente'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'referente.label', default: 'Referente'), id])
            redirect(action: "show", id: id)
        }
    }
	
	def listAdvancedJSON = {
	def sortIndex = params.sidx ?: 'nombre'
		def sortOrder  = params.sord ?: 'asc'
		def maxRows = Integer.valueOf(params.rows)
		def currentPage = Integer.valueOf(params.page) ?: 1
		def rowOffset = currentPage == 1 ? 0 : (currentPage - 1) * maxRows
		
		def referente = Referente.createCriteria().list(max: maxRows, offset: rowOffset) {
			
			if (params.nombre) ilike('nombre', params.nombre)
			
			order(sortIndex, sortOrder)
		}
		
		def totalRows = referente.totalCount
		def numberOfPages = Math.ceil(totalRows / maxRows)
		
		def results = referente?.collect {
			
			[
				cell: [it.nombre, it.direccion, it.telefono],
				 id: it.id
			]
		}

		
		def jsonData = [rows: results, page: currentPage, records: totalRows, total: numberOfPages]
		
		render jsonData as JSON
	}
	
	def listJson = {
		
		def sortIndex = params.sidx ?: 'nombre'
		def sortOrder  = params.sord ?: 'asc'
		def maxRows = Integer.valueOf(params.rows)
		def currentPage = Integer.valueOf(params.page) ?: 1
		def rowOffset = currentPage == 1 ? 0 : (currentPage - 1) * maxRows
		
		def referente = Referente.createCriteria().list(max: maxRows, offset: rowOffset) {
			if (params.nombre) eq('nombre', params.nombre.toLong())
			order(sortIndex, sortOrder)
		}
		
		def totalRows = referente.totalCount
		def numberOfPages = Math.ceil(totalRows / maxRows)
		
		def results = referente?.collect {
			[
				cell: [it.nombre, it.direccion, it.telefono, it.id],
				 id: it.id
			]
		}
	
		def jsonData = [rows: results, page: currentPage, records: totalRows, total: numberOfPages ]
		render jsonData as JSON
	
	}
	
}
